import java.io.RandomAccessFile;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Map;

public class Rate extends Thread {
	long length = 0;
	long download = 0;
	long undownload = 0;
	long mRate = 0;
	int calcSec=5;
	String rate;
	String completed;
	String timeRemain ;
	NumberFormat nf = NumberFormat.getInstance();
	ArrayList<BlockDownloader> list;
	Downloader downloader;
	RandomAccessFile bprser;
	
	XTableModel ingTableModel;
	XCompletedTableModel completedTableModel;
	int ingRowIndex;
	int completedIndex;
	public Rate(Downloader downloader, ArrayList<BlockDownloader> list2,
			long length2) throws Exception {
		this.downloader=downloader;
		length = length2;
		this.list = list2;
		Record.addDownloading(downloader.args);
		for (BlockDownloader bdownloader : list) {
			download += bdownloader.length();
		}
		nf.setMinimumFractionDigits(2);
		bprser=downloader.bprser;
	}

	public void run() {
		try {
			Map<Character, Long> bprMap=downloader.bprMap;
			int countSec=1;
			long t = 0;
			while (true) {
				if(downloader.thread_total==0){
					downloader.bprser.close();
					downloader.bpr.delete();
					System.out.println("completed: 100%" + " Rate: 0k/s need time: 00:00:00" );
					ingTableModel.delRow(ingRowIndex);
					completedTableModel.addRow(new String[]{downloader.file.getName(),downloader.file.getPath()});
					Record.delDownloading(downloader.args);
					return;
				}
				t = 0;
				countSec=1;
				bprser.seek(0);
				for (BlockDownloader ddownloader : list) {
					bprMap.put(ddownloader.getFname(), ddownloader.length());
					bprser.writeChar(ddownloader.getFname());
					bprser.writeLong(ddownloader.length());
					t += ddownloader.length();
				}
				
				if(downloader.args.length>4){
					sleep(3000);
					continue;
				}
				mRate = (t - download)/calcSec;
				if (mRate == 0) {
					sleep(1000);
					continue;
				}
				download = t;
				undownload = length - t;
				float tcompleted = (float) ((double) download / (double) length) * 100;
				double need_time = undownload / mRate;
				while (countSec <= calcSec) {
					 timeRemain = (int) (need_time / 3600) + ":"
												+ (int) (need_time % 3600) / 60 + ":"
												+ (int) (need_time % 60);
					this.completed=""+nf.format(tcompleted) + "%";
					rate = nf.format(mRate / 1024);
					if(ingTableModel!=null){
						ingTableModel.setValueAt(completed, ingRowIndex, 1);
						ingTableModel.fireTableCellUpdated(ingRowIndex,1);
						ingTableModel.setValueAt(rate, ingRowIndex, 2);
						ingTableModel.fireTableCellUpdated(ingRowIndex, 2);
						ingTableModel.setValueAt(timeRemain, ingRowIndex, 3);
						ingTableModel.fireTableCellUpdated(ingRowIndex, 3);
					}
					//System.out.println("completed: " + completed + " Rate: " + rate + "k/s" + " need time:" + timeRemain);
					++countSec;
					sleep(900);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Object get(int i){
		if(i==0)return downloader.file.getName();
		if(i==1)return completed;
		if(i==2)return rate;
		return timeRemain;
		
	}

	public void setIngTableModel(XTableModel tableModel, int rowIndex){
		this.ingRowIndex=rowIndex;
		this.ingTableModel=tableModel;
		//setValueAt(Object aValue, int rowIndex, int columnIndex) 
	}

	public void setCompletedTableModel(XCompletedTableModel completedTableModel,int completedIndex) {
		this.completedTableModel = completedTableModel;
		this.completedIndex=completedIndex;
	}
	
}
